<template>
  <div class="system-user-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1000px">
      <el-form ref="orgTenantDialogFormRef" :model="state.ruleForm" :rules="rules" size="default" label-width="auto">
        <el-row :gutter="35">
          <el-col :span="24" class="mb20">
            <el-form-item label="教育局" prop="orgId">
              <el-tree-select
                  v-model="state.ruleForm.orgId"
                  :data="state.sysOrgData"
                  :render-after-expand="true"
                  style="width: 500px"
                  check-strictly
                  filterable
                  show-all-levels
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="35">
          <el-col :span="24" class="mb20">
            <el-form-item label="租户id" prop="tenantId">
              <el-input v-model="state.ruleForm.tenantId"
                        placeholder="请输入租户id"
                        clearable>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(orgTenantDialogFormRef)" size="default">{{
              state.dialog.submitTxt
            }}</el-button>
				</span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup lang="ts" name="orgTenantDialog">
import {nextTick, onMounted, reactive, ref} from 'vue';
import type {FormRules, UploadProps, FormInstance} from 'element-plus'
import {ElMessage, UploadUserFile} from "element-plus";

import {useOrgTenantApi} from "/@/api/orgTenant";

const {add, edit} = useOrgTenantApi();
// 定义变量内容
const orgTenantDialogFormRef = ref<FormInstance>();
const state = reactive({
  sysOrgData: [] as OrgTreeType[],
  ruleForm: {
    id: '',
    orgId: '',
    tenantId: '',
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  sysOrgTreeData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 2100,
    },
  },
});
const fileList = ref<UploadUserFile[]>([])

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const props = defineProps(['orgTree'])

const rules = reactive<FormRules<RowOrgTenantType>>({
  orgId: [
    {required: true, message: '请选择一个教育局', trigger: 'blur'},
  ],
  tenantId: [
    {required: true, message: '请填写应用主页', trigger: 'blur'},
  ]
})


// 打开弹窗
const openDialog = async (type: string, row: RowOrgTenantType) => {
  state.sysOrgData = props.orgTree;
  if (type === 'edit') {
    state.dialog.title = '修改机构租户关系';
    state.dialog.submitTxt = '修 改';
    state.ruleForm = row;
  } else {
    state.dialog.title = '新增机构租户关系';
    state.dialog.submitTxt = '新 增';

    await nextTick(() => {
      if (orgTenantDialogFormRef.value) {
        orgTenantDialogFormRef.value.resetFields();
      }
    });
  }
  state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (!valid) {
      console.log('error submit!!', fields);
      ElMessage.error('数据校验失败!');
      return false;
    }
    const res = state.ruleForm.id ? await edit(state.ruleForm) : await add(state.ruleForm)
    if (res.status === 200) {
      ElMessage.success(state.ruleForm.id ? '更改成功' : '新增成功');
    }
    closeDialog();
    emit('refresh');
  });

  // if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
<style scoped lang="scss">
.croppers-container {
  .cropper-img-warp {
    text-align: center;

    .cropper-img {
      margin: auto;
      width: 150px;
      height: 150px;
      border-radius: 100%;
    }
  }
}
</style>
